package com.hoperun.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.hoperun.bean.AdminManager;
import com.hoperun.bean.AdminPermissions;
import com.hoperun.dao.AdminPermissionsDAO;
import com.hoperun.framework.ex.XHZException;
import com.hoperun.framework.ibatis.page.BootGrid;
import com.hoperun.framework.ibatis.page.BootGridPage;
import com.hoperun.framework.query.Criteria;
import com.hoperun.framework.util.Constant;
import com.hoperun.service.IAdminPermissionsSV;
import com.hoperun.utils.ErrorCode;
import com.hoperun.utils.LoginHelper;
import com.hoperun.utils.Constant.RecordState;

@Service("adminPermissionsSV")
public class AdminPermissionsSVImpl implements IAdminPermissionsSV {
	@Resource
	private AdminPermissionsDAO adminPermissionsDAO;

	@Override
	public void delete(Long id) throws Exception {
		if (null == id) {
			throw new XHZException(ErrorCode.ILLEGAL_REQUEST.ERROR_REQUEST);
		}
		Criteria criteria = new Criteria();
		criteria.addEqual(AdminPermissions.COL_PARENT_PERM_ID, id, Criteria.AND);
		criteria.addEqual(AdminPermissions.COL_STATUS, RecordState.STS_USE,
				Criteria.AND);
		criteria.limit(Constant.ZERO, Constant.ONE);

		List<AdminPermissions> list = queryByCondition(criteria);

		if (CollectionUtils.isNotEmpty(list)) {
			throw new XHZException(ErrorCode.PERMISSIONS.HAS_CHILD);
		}
		adminPermissionsDAO.delete(id);
	}

	@Override
	public AdminPermissions queryById(Long id) throws Exception {
		return adminPermissionsDAO.queryById(id);
	}

	@Override
	public AdminPermissions save(AdminPermissions adminPermissions)
			throws Exception {
		AdminManager adminManager = LoginHelper.getLoginUser();
		adminPermissions.setCreatime(new Date());
		adminPermissions.setCreator(adminManager.getManagerId());
		adminPermissions.setVersion(Constant.ONE);
		adminPermissions.setStatus(RecordState.STS_USE);
		adminPermissionsDAO.insert(adminPermissions);
		return adminPermissions;
	}

	@Override
	public void update(AdminPermissions adminPermissions) throws Exception {
		if (null == adminPermissions || null == adminPermissions.getPermId()) {
			throw new XHZException(ErrorCode.ILLEGAL_REQUEST.ERROR_REQUEST);
		}
		adminPermissions.setPermCode(null);
		adminPermissions.setParentPermId(null);
		AdminManager adminManager = LoginHelper.getLoginUser();
		adminPermissions.setUpdatetime(new Date());
		adminPermissions.setUpdator(adminManager.getManagerId());
		adminPermissions.setVersion(adminPermissions.getVersion()+Constant.ONE);
		adminPermissionsDAO.update(adminPermissions);
	}

	@Override
	public void saveOrUpdate(AdminPermissions adminPermissions)
			throws Exception {
		if (adminPermissions == null)
			return;
		if (StringUtils.isNotBlank("" + adminPermissions.getPermId())) {
			update(adminPermissions);
		} else {
			save(adminPermissions);
		}
	}

	@Override
	public void saveOrUpdate(List<AdminPermissions> list) throws Exception {
		if (CollectionUtils.isNotEmpty(list)) {
			for (AdminPermissions adminPermissions : list) {
				saveOrUpdate(adminPermissions);
			}
		}
	}

	@Override
	public List<AdminPermissions> queryByCondition(Criteria criteria)
			throws Exception {
		return adminPermissionsDAO.queryByCondition(criteria);
	}

	@Override
	public int queryCountByCondition(Criteria criteria) throws Exception {
		return adminPermissionsDAO.queryCountByCondition(criteria);
	}

	@Override
	public BootGridPage<AdminPermissions> queryPage(Criteria criteria,
			BootGrid<AdminPermissions> grid) throws Exception {

		// AdminPermissions model = grid.getEntity();

		int totalCount = adminPermissionsDAO.queryCountByCondition(criteria);

		if (Constant.ZERO < totalCount) {

			Map<String, String> sorts = grid.getSort();

			/** 设置排序规则 **/
			if (MapUtils.isNotEmpty(sorts)) {
				Set<String> keySets = sorts.keySet();
				String sort = "";
				for (String key : keySets) {
					sort += " , " + key + " " + sorts.get(key);
				}
				criteria.orderBy(sort.replaceFirst(",", ""));
			}
			if (grid.getCurrent() >= Constant.ZERO
					&& grid.getRowCount() > Constant.ZERO) {
				int start = (grid.getCurrent() - Constant.ONE)
						* grid.getRowCount();
				int size = grid.getRowCount();

				criteria.limit(start, size);
			}
			List<AdminPermissions> list = adminPermissionsDAO
					.queryByCondition(criteria);
			BootGridPage<AdminPermissions> page = new BootGridPage<AdminPermissions>(
					grid.getCurrent(), grid.getRowCount(), totalCount, list);

			return page;
		}
		return new BootGridPage<AdminPermissions>(grid.getCurrent(),
				grid.getRowCount(), totalCount,
				new ArrayList<AdminPermissions>());

	}

}
